﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CoolCode;
using CoolCode.Models;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Web.Mvc;
using TopTen.Entities;
using TopTen.Services;
using TopTen.Models;

namespace TopTen.UI.Controllers
{
	[HandleError]
	public class HomeController : CoolCode.Web.Mvc.ControllerBase
	{
		public ActionResult Index()
		{
			Title = "TopTen网";
			ViewData["Message"] = "Welcome to ASP.NET MVC!";

			return View();
		}

		public ActionResult About()
		{
			return View();
		}


		#region Login

		public ActionResult Login()
		{
			Title = "用户登录";
			LoginModel model = new LoginModel
			{
				SubTitle = "用户登录",
				RememberMe = false
			};
			return View(model);
		}

		[HttpGet]
		public ActionResult ValidateUser(LoginModel model, string returnUrl)
		{
			bool success = MembershipService.ValidateUser(model.UserName, model.Password);
			//System.Threading.Thread.Sleep(new Random().Next(4000));
			if (success)
			{
				FormsAuthenticationService.SignIn(model.UserName, model.RememberMe);
				//TODO:log who sign in the system...
				return this.Success();
			}
			else
			{
				return this.Fail("用户名或密码错误！");
			}
		}

		#endregion

		#region Register

		public ActionResult Register()
		{
			initial_Register();

			return View();
		}

		[HttpPost]
		public ActionResult Register(RegisterModel model)
		{
			if (ModelState.IsValid)
			{
				// Attempt to register the user
				MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

				if (createStatus == MembershipCreateStatus.Success)
				{
					FormsAuthenticationService.SignIn(model.UserName, false /* createPersistentCookie */);
					return RedirectToAction("Index", "Home");
				}
				else
				{
					ModelState.AddModelError("", ErrorCodeToString(createStatus));
				}
			}

			initial_Register();
			// If we got this far, something failed, redisplay form
			return View(model);
		}

		private void initial_Register()
		{
			Title = "创建新用户";
			ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
		}

		private static string ErrorCodeToString(MembershipCreateStatus createStatus)
		{
			// See http://go.microsoft.com/fwlink/?LinkID=177550 for
			// a full list of status codes.
			switch (createStatus)
			{
				case MembershipCreateStatus.DuplicateUserName:
					return "Username already exists. Please enter a different user name.";

				case MembershipCreateStatus.DuplicateEmail:
					return "A username for that e-mail address already exists. Please enter a different e-mail address.";

				case MembershipCreateStatus.InvalidPassword:
					return "The password provided is invalid. Please enter a valid password value.";

				case MembershipCreateStatus.InvalidEmail:
					return "The e-mail address provided is invalid. Please check the value and try again.";

				case MembershipCreateStatus.InvalidAnswer:
					return "The password retrieval answer provided is invalid. Please check the value and try again.";

				case MembershipCreateStatus.InvalidQuestion:
					return "The password retrieval question provided is invalid. Please check the value and try again.";

				case MembershipCreateStatus.InvalidUserName:
					return "The user name provided is invalid. Please check the value and try again.";

				case MembershipCreateStatus.ProviderError:
					return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

				case MembershipCreateStatus.UserRejected:
					return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

				default:
					return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
			}
		}

		#endregion
	}
}
